package com.it.aop;

import org.aspectj.lang.ProceedingJoinPoint;

import java.util.Arrays;

/*
定义切面类：用于存放增强代码的类
 */
public class MyAspect {

    public void beforeAdvice() {
        System.out.println("前置通知  run....");
    }

    public void afterReturning() {
        System.out.println("后置通知 run。。。");
    }

    public void afterThrowing() {
        System.out.println("异常通知 run。。。。");
    }

    public void afterAdvice() {
        System.out.println("最终通知 run。。。。");
    }

    /*
    环绕通知
    需要引入目标方法的包装对象 ProceedingJoinPoint
     */
    public Object around(ProceedingJoinPoint pjp) {
        //获取方法的入参
        Object[] args = pjp.getArgs();
        System.out.println(Arrays.toString(args));

        Object proceed = null;
        long start = System.currentTimeMillis();
        try {
            System.out.println("前置通知。。。");
            proceed = pjp.proceed(args);
            System.out.println("后置通知。。。");
        } catch (Throwable throwable) {
            System.out.println("异常通知。。。");
        } finally {
            System.out.println(" 最终通知");
        }
        long end = System.currentTimeMillis();
        System.out.println(end-start);

        return proceed;
    }
}
